package querydatabaseselection.selector;

import java.util.ArrayList;
import java.util.List;

import distributedRE.model.DataBase;
import distributedRE.model.DistributedREModel;
import distributedRE.model.Query;
import distributedRE.model.Session;

import querydatabaseselection.pair.QueryDatabasePair;
import rankingGenerator.RankingGenerator;
import rankingGenerator.statistics.DREComparableStatistic;

public abstract class QueryDatabaseSelector {

	public List<QueryDatabasePair> getNextSelection(DistributedREModel dREModel,RankingGenerator rG, Session s) {
		
		rG.startNewSession();
		
		ArrayList<DataBase> databases = dREModel.getDatabases();
		
		for (DataBase dataBase : databases) {
			
			ArrayList<Query> queries = dREModel.getQueries(dataBase);
		
			for (Query query : queries) {
				
				DREComparableStatistic value = rG.calculateCostValue(query,dataBase,dREModel,s);
				
				rG.storeCostValue(query,dataBase,value);
				
			}
		
		}
		
		rG.generateRanking();
		
		return this.select(rG.calculatedValues(),dREModel);
	}

	protected abstract boolean isDescending();

	protected abstract List<QueryDatabasePair> select(
			ArrayList<QueryDatabasePair> calculatedValues, DistributedREModel dREModel);

}
